<template>
  <div class="dashboard-container">
    <div class="app-container">

      <page-tools>
        <template slot="before">{{ page.total }}</template>
        <el-button type="primary" size="small" @click="$router.push('/import')">导入</el-button>
        <el-button type="primary" size="small" @click="exportData">导出</el-button>
        <el-button type="primary" size="small" @click="$refs['add-employee'].showDialog=true">新增员工</el-button>

      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="list">
          <el-table-column type="index" label="序号" />
          <el-table-column prop="username" label="姓名" sortable="" />
          <el-table-column prop="workNumber" label="工号" sortable="" />
          <el-table-column label="聘用形式" sortable :formatter="formatEmployment" prop="formOfEmployment" />
          <el-table-column prop="departmentName" label="部门" sortable="" />
          <el-table-column label="入职时间" sortable="" align="center">
            <!-- 作用域插槽 -->
            <template slot-scope="{ row }">{{ row.timeOfEntry | formatDate }}</template>
          </el-table-column>

          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template slot-scope="{ row }">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="deleteEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
          :current-page.sync="page.page"
          :page-sizes="[3, 4, 8, 10]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          background
          @size-change="sizeChange"
          @current-change="currentChange"
        >
          :pager-count="7">
        </el-pagination>

      </el-card>
    </div>
    <add-employee ref="add-employee" @update-list="getEmployeeList" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
import AddEmployee from './components/add-employee'
export default {
  name: 'Employees',
  components: {
    AddEmployee
  },
  data() {
    return {
      page: {
        page: 1,
        size: 3,
        total: 0
      },
      list: []
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      const { total, rows } = await getEmployeeList(this.page)
      //   console.log(res)
      this.page.total = total
      this.list = rows
    //   console.log(rows)
    },
    currentChange(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getEmployeeList()
    },
    sizeChange(newPage) {
      // newPage是当前点击的页码
      this.page.size = newPage // 将当前页码赋值给当前的最新页码
      this.getEmployeeList()
    },
    formatEmployment(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    async deleteEmployee(id) {
      try {
        await this.$confirm('确认删除？')
      } catch (error) {
        return console.log(error)
      }
      await delEmployee(id)
      this.$message.success('删除成功！')
      this.getEmployeeList()
    },
    exportData() {
      import('@/vendor/Export2Excel').then(async excel => {
        const { rows } = await getEmployeeList({
          page: 1,
          size: 1000
        })
        const headers = {
          '手机号': 'mobile',
          '姓名': 'username',
          '入职日期': 'timeOfEntry',
          '聘用形式': 'formOfEmployment',
          '转正日期': 'correctionTime',
          '工号': 'workNumber',
          '部门': 'departmentName'
        }
        excel.export_json_to_excel({
          header: Object.keys(headers), // 表头 必填
          data: this.formatJson(headers, rows), // 具体数据 必填
          filename: '员工信息表', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
        console.log(rows)
      })
    },
    formatJson(header, data) {
      const results = data.map(val => {
        return Object.values(header).map(key => {
          return val[key]
        })
      })
      console.log(results)
      return results
    }

  }
}
</script>

<style>

</style>
